
#ifndef __JPEG_H__
#define __JPEG_H__

#define JPEG_DEBUG  printf


/* JPEG marker codes */
enum JpegMarker {
    /* start of frame */
    SOF0  = 0xc0,       /* baseline 帧开始（标准 JPEG） */
    SOF1  = 0xc1,       /* extended sequential, huffman */
    SOF2  = 0xc2,       /* progressive, huffman */
    SOF3  = 0xc3,       /* lossless, huffman */

    SOF5  = 0xc5,       /* differential sequential, huffman */
    SOF6  = 0xc6,       /* differential progressive, huffman */
    SOF7  = 0xc7,       /* differential lossless, huffman */
    JPG   = 0xc8,       /* reserved for JPEG extension */
    SOF9  = 0xc9,       /* extended sequential, arithmetic */
    SOF10 = 0xca,       /* progressive, arithmetic */
    SOF11 = 0xcb,       /* lossless, arithmetic */

    SOF13 = 0xcd,       /* differential sequential, arithmetic */
    SOF14 = 0xce,       /* differential progressive, arithmetic */
    SOF15 = 0xcf,       /* differential lossless, arithmetic */

    DHT   = 0xc4,       /* define huffman tables 定义 Huffman 表（霍夫曼表） */

    DAC   = 0xcc,       /* define arithmetic-coding conditioning */

    /* restart with modulo 8 count "m" */
    RST0  = 0xd0,
    RST1  = 0xd1,
    RST2  = 0xd2,
    RST3  = 0xd3,
    RST4  = 0xd4,
    RST5  = 0xd5,
    RST6  = 0xd6,
    RST7  = 0xd7,

    SOI   = 0xd8,       /* start of image 图像头 */
    EOI   = 0xd9,       /* end of image 图像尾 */
    SOS   = 0xda,       /* start of scan 扫描行开始 */
    DQT   = 0xdb,       /* define quantization tables 定义量化表 */
    DNL   = 0xdc,       /* define number of lines 定义扫描行数 */
    DRI   = 0xdd,       /* define restart interval 定义重新开始间隔 */
    DHP   = 0xde,       /* define hierarchical progression */
    EXP   = 0xdf,       /* expand reference components */

    APP0  = 0xe0,       /** 定义交换格式和图像识别信息 */
    APP1  = 0xe1,
    APP2  = 0xe2,
    APP3  = 0xe3,
    APP4  = 0xe4,
    APP5  = 0xe5,
    APP6  = 0xe6,
    APP7  = 0xe7,
    APP8  = 0xe8,
    APP9  = 0xe9,
    APP10 = 0xea,
    APP11 = 0xeb,
    APP12 = 0xec,
    APP13 = 0xed,
    APP14 = 0xee,
    APP15 = 0xef,

    JPG0  = 0xf0,
    JPG1  = 0xf1,
    JPG2  = 0xf2,
    JPG3  = 0xf3,
    JPG4  = 0xf4,
    JPG5  = 0xf5,
    JPG6  = 0xf6,
    SOF48 = 0xf7,       ///< JPEG-LS
    LSE   = 0xf8,       ///< JPEG-LS extension parameters
    JPG9  = 0xf9,
    JPG10 = 0xfa,
    JPG11 = 0xfb,
    JPG12 = 0xfc,
    JPG13 = 0xfd,

    COM   = 0xfe,       /** 注释 */

    TEM   = 0x01,       /* temporary private use for arithmetic coding */

    /* 0x02 -> 0xbf reserved */
};




#define PREDICT(ret, topleft, top, left, predictor)\
    switch(predictor){\
        case 0: ret= 0; break;\
        case 1: ret= left; break;\
        case 2: ret= top; break;\
        case 3: ret= topleft; break;\
        case 4: ret= left   +   top - topleft; break;\
        case 5: ret= left   + ((top - topleft)>>1); break;\
        case 6: ret= top + ((left   - topleft)>>1); break;\
        default:\
        case 7: ret= (left + top)>>1; break;\
    }


/**
 * @brief 每一段的开头
 */
struct jpeg_header
{
    uint8_t type;           /** 段类型   */
    uint8_t identifier;     /** 段标识符 FF */
} __attribute__ ((__packed__));


/**
 * @brief 图像识别信息,  段类型: E0
 */
struct jpeg_img_identifier
{
    uint16_t len;               /** 段长度 */
    uint8_t  format[5];         /** 交换格式: 'JFIF' */
    uint8_t  major_version;     /** 主版本号 */
    uint8_t  minor_version;     /** 次版本号 */
    uint8_t  density;           /** 密度单位 */
    uint16_t x_density;         /** X像素密度 */
    uint16_t y_density;         /** Y像素密度   */
    uint8_t  _x_density;         /** 缩略图X像素  */
    uint8_t  _y_density;         /** 缩略图Y像素    */
    uint8_t  x_density;         /** 交换格式: 'JFIF' */
    uint8_t  x_density;         /** 交换格式: 'JFIF' */

};


/**
 * @brief 注释信息
 */
struct jpeg_commit
{
    uint16_t len;           /** 段长度 */
    uint8_t  *data;         /** 交换格式: 'JFIF' */
};



#endif
